<%@page import="java.util.HashMap"%>
<%@page import="java.util.Enumeration"%>
<%@page import="com.cinemtl.beans.CartOrder"%>
<%@page import="com.cinemtl.beans.User"%>
<%@page import="com.cinemtl.action.UserAction"%>
<%@page import="com.cinemtl.beans.Movie"%>
<%@page import="com.cinemtl.manager.MovieManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	
<%
	if(session.getAttribute("user") != null){
		User user = (User) session.getAttribute("user");
		if(user.getUsername().equals("admin")){
	request.getRequestDispatcher("admin").forward(request,response);
		}
	}
%>	

<%
	Movie movie = null;
	if (request.getParameter("id") != null) {
		int id = Integer.valueOf(request.getParameter("id"));
		movie = MovieManager.getById(id);

	}else{
		response.sendError(404, "movie not found");
	}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<%
	if(request.getParameter("id") != null){
%>
<title>Cine-MTL | <%=movie.getTitle()%></title>
<%
	}else{
%>
<title>Cine-MTL | Movie</title>
<%
	}
%>
<link href='http://fonts.googleapis.com/css?family=Open+Sans'
	rel='stylesheet' type='text/css'>
<link rel="stylesheet"
	href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="css/home.css" type="text/css" />
<link rel="stylesheet" href="css/flat-ui.min.css" type="text/css" />
</head>
<style>
img {
	border-radius: 10px;
}
</style>

</head>
<body>

	<div class="container">
		<div class="row">
			<header>
				<a id="logo" href="home.jsp"><img src="images/cine-mtl-logo.png"></img></a>
				<div id="welcome"
					class="col-xs-offset-3 col-sm-offset-4 col-md-offset-6 col-lg-offset-7">
					<%
						if (session.getAttribute("user") != null) {
								User user = (User) session.getAttribute("user");
					%>

					<div id="showUsername">
						<span id="nameUser">Welcome <%=user.getUsername()%>
						</span><span class="fui-exit" id="logout"></span>
					</div>

					<%
						} else {
					%>
					<div id="showUsername">
						<span id="nameUser">Welcome Guest</span>
					</div>

					<%
						}
					%>
					<%
						Enumeration<String> keys=session.getAttributeNames();
							HashMap<Integer,CartOrder> orders = new HashMap<Integer,CartOrder>();
							while(keys.hasMoreElements())
							{
								if (keys.nextElement().equals("cart")) {
									orders = (HashMap<Integer, CartOrder>) session
											.getAttribute("cart");
								}
							}

							if (orders.isEmpty()) {
					%>
					<div id="cart">
						<span class="glyphicon glyphicon-shopping-cart" id="cartButton"><a
							href="cart.jsp" id="cartLink" data-qty="0">Cart(0)</a></span>
					</div>
					<%
						}else
							{
							
							
							int somme = 0;
							for(CartOrder order: orders.values())
							{
								somme+=order.getNbTicket();
							}
					%>
					<div id="cart">
						<span class="glyphicon glyphicon-shopping-cart" id="cartButton"><a
							href="cart.jsp" id="cartLink" data-qty="<%=somme%>">Cart(<%=somme%>)
						</a></span>
					</div>
					<%
						}
					%>
				</div>
				<nav class="navbar navbar-default navbar-embossed" role="navigation">
					<div class="navbar-header">
						<button type="button" class="navbar-toggle" data-toggle="collapse"
							data-target="#bs-example-navbar-collapse-1">
							<span class="sr-only">Toggle navigation</span> <span
								class="icon-bar"></span> <span class="icon-bar"></span> <span
								class="icon-bar"></span>
						</button>
					</div>
					<div class="collapse navbar-collapse"
						id="bs-example-navbar-collapse-1">
						<ul class="nav navbar-nav">
							<li class="active"><a href="home.jsp">Home</a></li>

							<li class="active"><a href="movies.jsp">Movies</a></li>
							<li><a href="contact.jsp">Contact</a></li>
						</ul>
						<form class="navbar-form navbar-right" action="search"
							method="get" role="search">
							<div class="form-group">
								<div class="input-group">
									<input class="form-control" id="navbarInput-01" type="search"
										name="search" placeholder="Search"> <span
										class="input-group-btn">
										<button type="submit" class="btn">
											<span class="fui-search"></span>
										</button>
									</span>
								</div>
							</div>
						</form>
						<%
							if (session.getAttribute("user") == null) {
						%>
						<div id="inout">
							<ul class="nav navbar-nav navbar-right">
								<li><a href="#" data-toggle="modal"
									data-target="#loginModal" class="">Log in</a></li>
								<li><a href="register.html">Sign up</a></li>
							</ul>
						</div>
						<%
							}
						%>
					</div>

				</nav>
			</header>
			<div class="container">

				<%
					if (request.getParameter("id") != null) {
				%>

				<div class="jumbotron">
					<div class="row">
						<div class="col-xs-4 col-sm-4 col-md-4 col-lg-4 pull-left">
							<img class="img-responsive" src="<%=movie.getUrlImage()%>" alt="">
						</div>
						<div class="col-xs-8 col-sm-8 col-md-8 col-lg-8">

							<h4 class="pull-right">$9.98</h4>
							<h4>
								<a><%=movie.getTitle()%></a>
							</h4>
							<h5>
								<a><%=movie.getType()%></a>
							</h5>
							<p><%=movie.getDescription()%></p>
							<a href="<%=movie.getUrlVideo()%>" class="btn btn-md btn-danger">Play
								Trailer</a> <a data-movie="<%=movie.getId()%>"
								class="btn btn-md btn-warning addCartMovies">Add Cart</a>

						</div>
					</div>
				</div>

				<%
					}
				%>



			</div>
			<div class="modal fade" id="loginModal" tabindex="-1" role="dialog"
				aria-labelledby="myModalLabel" aria-hidden="true">
				<div class="modal-dialog">
					<div class="modal-content">
						<div class="modal-header">
							<button type="button" class="close" data-dismiss="modal"
								aria-hidden="true">&times;</button>
							<h4 class="modal-title">Log in</h4>
						</div>

						<div class="modal-body">
							<form id="loginForm" method="post" class="form-horizontal"
								action="">
								<div class="login-form">
									<div class="form-group">
										<input type="text" class="form-control login-field"
											name="username" placeholder="Enter your name" id="login-name"
											required /> <label class="login-field-icon fui-user"
											for="login-name"></label>
									</div>

									<div class="form-group">
										<input type="password" class="form-control login-field"
											name="password" placeholder="Password" id="login-pass"
											required /> <label class="login-field-icon fui-lock"
											for="login-pass"></label>
									</div>
									<input class="btn btn-primary btn-lg btn-block" type="button"
										id="loginMovie" value="login"> <a class="login-link"
										href="redirect">Lost your password?</a>

								</div>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
		<footer>
			<div class="row">
				<div class="col-lg-12">
					<h5>Copyright &copy; Cine-MTL 2015</h5>
				</div>
			</div>
		</footer>
	</div>
	<script
		src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
	<script src="js/flat-ui.min.js"></script>
	<script type="text/javascript" src="js/sha1.js"></script>
	<script src="js/main.js"></script>
	<script src="js/cart.js"></script>
	<script>
		$('.input-group').on('focus', '.form-control', function() {
			$(this).closest('.input-group, .form-group').addClass('focus');
		}).on('blur', '.form-control', function() {
			$(this).closest('.input-group, .form-group').removeClass('focus');
		});
	</script>


</body>
</html>